package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author 杨佳畅
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private CoursePubService coursePubService;

    @PostMapping("course/list")
    public PageVO queryCourseBaseList(PageRequestParams params, @RequestBody QueryCourseBaseModel model) {
        // 获取公司ID
        Long companyId = UAASecurityUtil.getCompanyId();
        return courseBaseService.queryCourseBaseList(params, model, companyId);
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        // 获取公司ID
        Long companyId = UAASecurityUtil.getCompanyId();
        // 将VO转为DTO
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        // 设置公司ID
        dto.setCompanyId(companyId);
        CourseBaseDTO result = courseBaseService.createCourseBase(dto);
        return result;
    }

    @GetMapping("course/{courseId}")
    public CourseBaseDTO queryCourseBaseById(@PathVariable("courseId") Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        CourseBaseDTO dto = courseBaseService.getCourseBaseById(courseBaseId, companyId);
        return dto;
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO vo) {
        // 获取公司ID
        Long companyId = UAASecurityUtil.getCompanyId();
        // 将VO转为DTO
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);

        CourseBaseDTO result = courseBaseService.modifyCourseBase(dto);
        return result;
    }

    @DeleteMapping("course/{courseId}")
    public void removeCourseBaseById(@PathVariable("courseId") Long courserBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courserBaseId, companyId);
    }

    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }

    /**
     * 课程预览
     *
     * @param courseId  课程ID
     * @param companyId 机构ID
     * @return
     */
    @GetMapping("course/preview/{courseId}/{companyId}")
    public Object previewCourse(@PathVariable Long courseId, @PathVariable Long companyId) {
        Map<String, Object> dataMap = coursePubService.previewCourse(courseId, companyId);
        ModelAndView modelAndView = new ModelAndView("learing_article", dataMap);
        return modelAndView;
    }

    /**
     * 课程发布
     *
     * @param courseId 课程ID
     */
    @PostMapping("course_pub/publish/{courseId}")
    public void publish(@PathVariable Long courseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        coursePubService.publish(courseId, companyId, false);
    }
}
